package com.zm.controller;

import com.alibaba.fastjson.JSON;
import com.zm.pojo.AppVersion;
import com.zm.service.AppVersionService;
import org.apache.log4j.Logger;
import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**App版本控制类
 * Created by Administrator on 2018/9/18.
 */
@Controller
@RequestMapping("/appversion")
public class AppVersionController {
    private Logger logger=Logger.getLogger(AppVersionController.class);
    @Resource
    private AppVersionService appVersionService;

    /**
     * 初始化新增版本数据
     * @param appVersion
     * @return
     */
    @RequestMapping("/add")
    public String add(@ModelAttribute ("appversion") AppVersion appVersion,Model model){
             List<AppVersion> list=appVersionService.selectBefore();
        logger.info("list>>>:"+list.get(0).getApkfilename()+">>>"+list.get(0).getPublisthstatusname()
    +">>>"+list.size());
              model.addAttribute("versionList",list);
        return "version/addappversion";
    }

    /**
     * 新增版本保存
     * @param appVersion
     * @param rs
     * @param apkfilename
     * @param model
     * @param session
     * @return
     * @throws IOException
     */
    @RequestMapping("/addAppversion")
    public String addAppversion(@Valid AppVersion appVersion, BindingResult rs, @RequestParam("apkfilename") MultipartFile apkfilename , Model model, HttpSession session) throws IOException {
//        if (rs.hasErrors()){
//            return "version/addappversion" ;
//        }
        appVersion.setCreationdate(new Date());
        //如果没有文件上传，MultipartFile ogpicpath也不会为null,可以通过调用getsize()方法获取文件的大小来判断是否有上传文件
        if (apkfilename.getSize()>0) {
            String path=session.getServletContext().getRealPath("images");
            String fileName=apkfilename.getOriginalFilename();
            if (fileName.endsWith("apk")) {
                File file=new File(path,fileName);
                apkfilename.transferTo(file);
            }
        }
        if (appVersionService.addVersion(appVersion)){
            logger.info("添加成功");
            return "redirect:/appversion/add";
        }
        logger.info("添加失败");
        return "403";
    }

    /**
     * 历史版本信息
     * @return
     */
//    @RequestMapping("/showappversion")
//    public Object selectHistoryVersion(){
//        List<AppVersion> list=appVersionService.selectBefore();
//        logger.info("list>>>:"+list.get(0).getApkfilename()+">>>"+list.get(0).getVersionno()
//        +">>>"+list.size());
//        Object o=JSON.toJSONString(list);
//        return o;
//    }
}
